#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: config.py
# Author: Tomás Vírseda
# License: GPL v3
# Description:  Configuration Manager

import os
from ConfigParser import ConfigParser

from vazaarlib.constants import USER_DIR, VAZAAR_USER_DIR
from vazaarlib.constants import GLOBAL_CONFIG_DIR, GLOBAL_CONFIG_FILE
from vazaarlib.constants import VAZAAR_CONFIG_DIR, VAZAAR_CONFIG_FILE
from vazaarlib.utils import get_logger


class ConfigManager:
    def __init__(self, app):
        self.app = app
        self.log = get_logger("Config")
        self.config = ConfigParser()
        self.config_file = self.check_environment()

        self.config.read(self.config_file)


    def check_environment(self):
        if not os.path.exists(GLOBAL_CONFIG_FILE):
            self.log.debug("Creating directories")
            try:
                os.makedirs(GLOBAL_CONFIG_DIR)
                self.log.debug("\tmkdir %s" % GLOBAL_CONFIG_DIR)
            except Exception, error:
                self.log.error("\t%s" % error)

            # write real config path  in global config file
            f1 = open(GLOBAL_CONFIG_FILE,'w')
            f1.write(VAZAAR_CONFIG_FILE)
            f1.close()
            #self.create_settings()
            #self.log.info("Created a new config file: %s" % VAZAAR_CONFIG_FILE)
            #print "from check_environment", self.config_file
            return VAZAAR_CONFIG_FILE
        else:
            # get GLOBAL_CONFIG_FILE which was written @ VAZAAR_CONFIG_FILE
            f = open(GLOBAL_CONFIG_FILE, "r")
            config_file = f.read()
            f.close()
            self.log.info("Using config file: %s" % config_file)
            return config_file


    def create_settings(self):
        self.config.add_section('Storage')
        self.config.set('Storage', 'backend', 'sqlite')
        self.config.set('Storage', 'default_uri', VAZAAR_CONFIG_DIR)
        self.write_config()

    def get_value(self, section, item):
        return self.config.get(section, item)


    def set_value(self, section, item, value):
        if not self.config.has_section(section):
            self.config.add_section(section)

        self.config.set(section, item, value)
        self.log.info("[%s] %s -> %s" % (section, item, value))


    def write_config(self):
        settings = open(self.config_file, 'w')
        self.config.write(settings)
        settings.close()
        #self.log.info("Config file written")

"""
from firstrun import check_environment

USER_DIR = os.environ['HOME'] + '/.vazaar'
CONFIG_FILE = USER_DIR + '/vazaar.conf'

config_file = check_environment()

Config = ConfigParser()
Config.read(config_file)

STORAGE_BACKEND = Config.get('Storage', 'backend')
if STORAGE_BACKEND == 'mysql':
    dbhost = Config.get('Storage', 'dbhost')
    dbuser = Config.get('Storage', 'dbuser')
    dbpass = Config.get('Storage', 'dbpass')
    dbname = Config.get('Storage', 'dbname')
    STORAGE_OPTIONS = "host=%s,user=%s,password=%s,db=%s" % (dbhost, dbuser, dbpass, dbname)
else:
    DEFAULT_URI = Config.get('Storage', 'default_uri')
    STORAGE_OPTIONS = DEFAULT_URI + '/.metadata/'
"""
